package swing;

import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

/**
 * @author du.senlin
 * @Title:
 * @Description:
 * @date 2020/3/30 14:51
 */
public class SwingPanel extends JFrame {
    private JRadioButton radioButtonCn, radioButtonTw;
    private JTextField jTextField, field1, field2, condition;
    private JPanel jPanel;
    private JLabel jLabel, conditionJLabel, jLabel1, jLabel2;
    private JButton jButton;
    private ButtonGroup buttonGroup;

    private static final String TITLE = "测试1";
    private static final String FONT_DIALOG = "Dialog";
    private static final String ZH_CN = "选项1";
    private static final String ZH_TW = "选项2";
    private static final String TABLE_INPUT_AST = "如: 1";
    private static final String CONDITION_INPUT_AST = "如: 2";
    private static final String FIELD1_INPUT_AST = "如: 3";
    private static final String FIELD2_INPUT_AST = "如: 4";


    public SwingPanel() {
        this.setTitle(TITLE);
        jPanel = new JPanel();
        setComponents(jPanel);
        this.add(jPanel);
        this.setSize(450, 300);
        // 设置界面可见
        this.setVisible(true);
        // 设置居中
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private void setComponents(JPanel panel) {
        // 设置布局为 null
        panel.setLayout(null);
        // 创建表名称文本域用于输入
        getTablejTextField(panel);
        // 创建条件文本域用于输入
        getConditionJLabel(panel);
        // 创建字段1文本域用于输入
        getField1(panel);
        // 创建字段2文本域用于输入
        getField2(panel);
        // 创建单选按钮
        createRadioButton(panel);
        // 创建确认按钮并监听提交事件
        submit(panel);
    }

    private void submit(JPanel panel) {
        jButton = new JButton("确定");
        jButton.setBounds(200, 180, 80, 25);
        panel.add(jButton);
        jButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String jTextTable = jTextField.getText().trim();
                String jText1Condition = condition.getText().trim();
                String jText1 = field1.getText().trim();
                String jText2 = field2.getText().trim();
                String enable = "";
                Enumeration<AbstractButton> radioBtns = buttonGroup.getElements();
                while (radioBtns.hasMoreElements()) {
                    AbstractButton btn = radioBtns.nextElement();
                    if (btn.isSelected()) {
                        enable = btn.getText();
                        break;
                    }
                }
                // 如果校验不通过直接报错提示
                if (verifyInputText(jTextTable, jText1Condition, jText1, jText2, enable)) {
                    return;
                }
            }
        });
    }








    private boolean verifyInputText(String jTextTable, String jText1Condition, String jText1, String jText2, String enable) {
        if (StringUtils.isBlank(enable)) {
            JOptionPane.showMessageDialog(null, "条件任意选择一个", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return true;
        }
        if (jText1Condition.equals(CONDITION_INPUT_AST) || StringUtils.isBlank(jText1Condition)) {
            JOptionPane.showMessageDialog(null, "条件1输入不合法，请重新输入", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return true;
        }
        if (jTextTable.equals(TABLE_INPUT_AST) || jTextTable.split("\\.").length < 2) {
            JOptionPane.showMessageDialog(null, "条件2输入不合法，请重新输入", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return true;
        }
        if (jText1.equals(FIELD1_INPUT_AST) || StringUtils.isBlank(jText1)) {
            JOptionPane.showMessageDialog(null, "条件3输入不合法，请重新输入", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return true;
        }
        if (jText2.equals(FIELD2_INPUT_AST) || StringUtils.isBlank(jText2)) {
            JOptionPane.showMessageDialog(null, "条件4输入不合法，请重新输入", "温馨提示", JOptionPane.ERROR_MESSAGE);
            return true;
        }
        return false;
    }

    private void createRadioButton(JPanel panel) {
        radioButtonCn = new JRadioButton(ZH_CN);
        radioButtonCn.setBounds(10, 180, 80, 25);
        panel.add(radioButtonCn);

        radioButtonTw = new JRadioButton(ZH_TW);
        radioButtonTw.setBounds(100, 180, 80, 25);
        panel.add(radioButtonTw);
        //添加进组里，使其只能选其中一个
        buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButtonCn);
        buttonGroup.add(radioButtonTw);
    }

    private void getField2(JPanel panel) {
        jLabel2 = new JLabel("属性2:");
        jLabel2.setFont(new Font(FONT_DIALOG, 1, 18));
        jLabel2.setBounds(10, 140, 80, 25);
        panel.add(jLabel2);

        field2 = new JTextField(20);
        if (StringUtils.isBlank(field2.getText())) {
            // 提示内容
            field2.setForeground(Color.red);
            field2.setText(FIELD2_INPUT_AST);
        }
        field2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Object source = e.getSource();
                if (source == field2) {
                    String text = field2.getText();
                    if (text.equals(FIELD2_INPUT_AST)) {
                        // 移入时，如果是提示内容，则清空
                        field2.setText("");
                        field2.setForeground(Color.black);
                    }
                }

            }

            @Override
            public void mouseExited(MouseEvent e) {
                Object source = e.getSource();
                if (source == field2) {
                    String text = field2.getText();
                    if (StringUtils.isBlank(text)) {
                        // 移出时没有发现内容,则设置为提示内容
                        field2.setForeground(Color.red);
                        field2.setText(FIELD2_INPUT_AST);
                    }
                }
            }
        });

        field2.setBounds(100, 140, 240, 25);
        panel.add(field2);
    }

    private void getField1(JPanel panel) {
        jLabel1 = new JLabel("属性1:");
        jLabel1.setFont(new Font(FONT_DIALOG, 1, 18));
        jLabel1.setBounds(10, 100, 80, 25);
        panel.add(jLabel1);

        // 创建文本域用于输入
        field1 = new JTextField(20);
        if (StringUtils.isBlank(field1.getText())) {
            // 提示内容
            field1.setText(FIELD1_INPUT_AST);
            field1.setForeground(Color.red);

        }
        field1.setBounds(100, 100, 240, 25);
        field1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Object source = e.getSource();
                if (source == field1) {
                    String field1Text = field1.getText();
                    if (field1Text.equals(FIELD1_INPUT_AST)) {
                        // 移入时，如果是提示内容，则清空
                        field1.setForeground(Color.black);
                        field1.setText("");
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Object source = e.getSource();
                if (source == field1) {
                    String text = field1.getText();
                    if (StringUtils.isBlank(text)) {
                        // 移出时没有发现内容,则设置为提示内容
                        field1.setText(FIELD1_INPUT_AST);
                        field1.setForeground(Color.red);
                    }
                }
            }
        });
        panel.add(field1);
    }

    private void getConditionJLabel(JPanel panel) {
        conditionJLabel = new JLabel("条件:");
        conditionJLabel.setFont(new Font(FONT_DIALOG, 1, 18));
        conditionJLabel.setBounds(10, 60, 80, 25);
        panel.add(conditionJLabel);

        condition = new JTextField(20);
        if (StringUtils.isBlank(condition.getText())) {
            // 提示内容
            condition.setText(CONDITION_INPUT_AST);
            condition.setForeground(Color.red);
        }
        condition.setBounds(100, 60, 240, 25);
        condition.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Object eSource = e.getSource();
                if (eSource == condition) {
                    String conditionText = condition.getText();
                    if (conditionText.equals(CONDITION_INPUT_AST)) {
                        // 移入时，如果是提示内容，则清空
                        condition.setForeground(Color.black);
                        condition.setText("");
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent mouseEvent) {
                Object source = mouseEvent.getSource();
                if (source == condition) {
                    String text = condition.getText();
                    if (StringUtils.isBlank(text)) {
                        // 移出时没有发现内容,则设置为提示内容
                        condition.setText(CONDITION_INPUT_AST);
                        condition.setForeground(Color.red);
                    }
                }
            }
        });
        panel.add(condition);
    }

    private void getTablejTextField(JPanel panel) {
        jTextField = new JTextField(20);
        jLabel = new JLabel("名称:");
        jLabel.setFont(new Font(FONT_DIALOG, 1, 18));
        jLabel.setBounds(10, 20, 80, 25);
        panel.add(jLabel);
        if (StringUtils.isBlank(jTextField.getText())) {
            // 提示内容
            jTextField.setText(TABLE_INPUT_AST);
            jTextField.setForeground(Color.red);

        }
        jTextField.setBounds(100, 20, 240, 25);
        jTextField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Object source = e.getSource();
                if (source == jTextField) {
                    String text = jTextField.getText();
                    if (text.equals(TABLE_INPUT_AST)) {
                        // 移入时，如果是提示内容，则清空
                        jTextField.setForeground(Color.black);
                        jTextField.setText("");
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Object source = e.getSource();
                if (source == jTextField) {
                    String text = jTextField.getText();
                    if (StringUtils.isBlank(text)) {
                        // 移出时没有发现内容,则设置为提示内容
                        jTextField.setText(TABLE_INPUT_AST);
                        jTextField.setForeground(Color.red);

                    }
                }
            }
        });
        panel.add(jTextField);
    }

}
